import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import '../constants/color_constants.dart';

/*
 * 图片裁剪引擎封装，目前使用的是 UCorp 框架
 */
class ImageCropEngine {

  /// 开启图片裁剪 - 返回裁剪后的图片的路径 (默认裁剪后的路径在App/Data/Cache目录下)
  static Future<String?> imageCrop(BuildContext context,
      String filePath, {
        CropAspectRatioPreset aspect = CropAspectRatioPreset.square,
      }) async {
    CroppedFile? croppedFile = await ImageCropper().cropImage(
      sourcePath: filePath,
      aspectRatioPresets: [
        aspect,
      ],
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: '图片裁剪'.tr,
            toolbarColor: hexToColor('#212121'),
            statusBarColor: hexToColor('#212121'),
            toolbarWidgetColor: Colors.white,
            initAspectRatio: CropAspectRatioPreset.original,
            lockAspectRatio: false),
        IOSUiSettings(
          title: '图片裁剪'.tr,
        ),
        WebUiSettings(
          context: context,
        ),
      ],
    );

    if (croppedFile != null) {
      return croppedFile.path;
    }

    return null;
  }
}
